package com.ceair.flight.refund.service.cronjob.impl;

import com.ceair.flight.refund.dto.appmoblie.RefundApplyDto;
import com.ceair.flight.refund.dto.external.BasePageQueryReq;
import com.ceair.flight.refund.dto.external.RefundApply;
import com.ceair.flight.refund.location.dt.enums.RefundStatus;
import com.ceair.flight.refund.service.cronjob.IScheduleService;
import com.ceair.flight.refund.service.external.IExceptionProcessorService;
import com.ceair.flight.refund.service.uatp.IGenRefNoService;
import com.ceair.flight.refund.util.DBUtil;
import com.ceair.flight.refund.util.RefundApplyConverUtil;
import com.ceair.flight.refund.util.RefundUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 处理生成退票单异常申请单
 *
 * @author youguilin
 */
@Service
public class AutoGenRefNoExcServiceImpl extends AbstractScheduleServiceImpl implements IScheduleService, IExceptionProcessorService {

    private static final Logger LOG = LoggerFactory.getLogger(AutoGenRefNoExcServiceImpl.class);
    //对所有7天以内生成退票单异常的申请单进行重新生成退票单
    private static final int HOURS_BEFORE = -24 * 7 * 8;
    @Autowired
    private IGenRefNoService genRefNoService;

    public void doCronJob() {
        LOG.info("Starting deal with generating refund No exception cron job");
        super.doScheduleTask();
        LOG.info("Finished deal with generating refund No exception cron job");
    }

    @Override
    protected void doScheduleTask(RefundApplyDto refundApply) {
        doException(RefundApplyConverUtil.refundApplyConverter(refundApply));
    }

    @Override
    protected BasePageQueryReq buildQueryReq() {
        BasePageQueryReq pageQueryReq = new BasePageQueryReq();
        pageQueryReq.addRefundStatus(RefundStatus.GENERATE_REFUND_NO_EXCEPTION);
        pageQueryReq.addRefundStatus(RefundStatus.SECOND_AUDIT_PASS);
        Date sysdate = new Date();
        pageQueryReq.setUpdateBegin(RefundUtil.getDate(sysdate, HOURS_BEFORE));
        pageQueryReq.setPageSize(20);
        return pageQueryReq;
    }

    @Override
    public boolean doException(RefundApply refundApply) {
        try {
            return genRefNoService.doGenRefNo(RefundApplyConverUtil.refundApplyConverter(refundApply));
        } catch (Exception ex) {
            LOG.error("Process generated refund No failed", ex);
            return false;
        }
    }
}
